* the script to extract ownership links from JSCReg (all firms in JSCReg)

* keep JSCReg entries with corresponding posting date
use "JSCReg.dta", clear
keep if D_POST == $maxdate
* limit entry date to everything after Jan 1, 1999
* mainly for posting date in 2002
keep if D_VNES >= 14245
save "JSCReg_working.dta", replace

* create a list of firms that are in JSCReg as of specified date 
contract okpo
drop _freq
rename okpo id
gen step = 0
gen name = ""
gen address = ""
gen country = 804
gen entity = 1
gen prev_id = .
save all_entities.dta, replace

* search for owners of the firms in JSCReg
* (in this case, multiple iterations are not needed:
* extracting immediate owners of all the firms in the JSCReg
* already creates the full list of edges 
* needed to construct ownership networks later)

* the okpo list for JSCReg search
keep if entity == 1 & country == 804
keep id name entity country
sort id
quietly by id:  gen dup = cond(_N==1,0,_n) 
drop if dup > 1
drop dup
rename name prev_name
rename country prev_country
rename entity prev_entity

* extract ownership links from JSCReg
rename id okpo
merge 1:m okpo using JSCReg_working.dta
keep if _merge==3

* drop individual owners
drop if O_URSTAT == "Фiзичнi особи резиденти" | O_URSTAT == "Фiз.особа-власник" | O_URSTAT=="Власник - фiзична особа" | O_URSTAT == "Фiзичнi особи нерезиденти"
drop if O_NAME=="Фiзичнi особи резиденти" | O_NAME=="Фiзичнi особи нерезиденти" | O_NAME=="Власник - фiзична особа" | O_NAME=="ФIЗИЧНI ОСОБИ РЕЗИДЕНТИ" | O_NAME=="ФIЗИЧНI ОСОБИ НЕРЕЗИДЕНТИ" | O_NAME=="Фiзична особа" | O_NAME=="ФIЗ.ОСОБА" | O_NAME=="Фiзична особа резидент"

* rename country code variable
rename country_code_ISO country

* drop Ukrainian owners with missing codes
drop if country == 804 & (investor_code==0 | missing(investor_code))

* create temporary codes for all foreign owners
* later these codes will be replaced with unique codes
* for the purpose of building the owner network
replace investor_code = 1234567890 if country != 804

* add other firm variables
rename okpo prev_id
rename investor_code id
gen entity = 1
gen owner = 1
rename O_NAME name
rename O_ADDRESS address
label var prev_id "id of entity from previous step"
label var prev_name "name of entity from previous step"
label var prev_country "country of entity from previous step"
label var prev_entity "type of entity from previous step"
label var owner "owner of previous step entity"
drop D_POST D_VNES _merge
order name entity country owner prev_id prev_name prev_entity prev_country O_URSTAT, after(id)
save corp_owners_from_JSCReg.dta, replace

* remove irrelevant owners
do JSCReg_clean.do

* append owners to the list of firms in JSCReg
gen source = "JSCReg"
gen step = 1
append using all_entities.dta
save all_entities.dta, replace

* check missing country codes
tab O_COUNTRY if missing(country)

* missing names
* use owner names from other entries for firms with same identifiers in JSCReg
sort id name
bysort id: replace name = name[_N] if missing(name)
save "all_entities_full_sample_${filedate}_JSCReg_only", replace

* if names are still missing, fill them in using identifiers and names from SReg data
* create a list of firms with missing names
keep if step == 0 & missing(name)
keep id
rename id okpo

* merge with the list of firm names and identifiers from SReg
merge 1:m okpo using SReg_firm_names.dta
keep if _merge != 2
contract okpo firm_name
drop _freq
sort okpo firm_name
bysort okpo: keep if _n==_N
rename okpo id
gen step = 0
gen entity = 1
gen country = 804

* add to other owners (with non-missing names), save data
merge m:m id entity country step using "all_entities_full_sample_${filedate}_JSCReg_only"
replace name = firm_name if missing(name) & step == 0
drop _merge firm_name
save "all_entities_full_sample_${filedate}_JSCReg_only", replace

* clean up data for network analysis

* fix codes/names for foreign owners
* load a list of foreign owners
use "all_entities_full_sample_${filedate}_JSCReg_only", clear
keep if id == 1234567890 & country != 804
contract id name country
drop _freq

* assign a unique identifier to each foreign owner name (1 id per country)
encode name, gen(temp_id)
tostring country temp_id, gen(new_country temp_id_str)
gen new_id = new_country + temp_id_str
destring new_id, replace
replace new_id = new_id + 99990000000
keep id new_id name country
merge 1:m id name country using "all_entities_full_sample_${filedate}_JSCReg_only"
replace id = new_id if id == 1234567890 & country != 804
drop new_id _merge

* set the oligarch dummy to missing (for compatibility with the data frame on ownership links from SReg)
gen oligarch = .

* save data
save "Ownership Data/all_entities_fin_${filedate}_full_sample_JSCReg_only", replace

* erase temporary files
erase all_entities.dta
erase corp_owners_from_JSCReg.dta
erase corp_owners_from_JSCReg_clean.dta
erase "all_entities_full_sample_${filedate}_JSCReg_only.dta"
